<?php

namespace App\listener\Customer;

use App\Events\Customer\CustomerSaved as Saved;
use App\model\customer\Customer;
use App\model\customer\CustomerArea;
use App\model\customer\CustomerHangye;
use App\model\customer\CustomerHuanping;
use App\model\customer\CustomerPeidian;
use App\model\customer\CustomerSource;
use App\model\Customer\CustomerWuye;
use App\model\Customer\CustomerXiaofang;
use App\model\Customer\CustomerXuqiu;
use App\User;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class CustomerSaved
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //

    }

    /**
     * Handle the event.
     *
     * @param  CustomerSaved  $event
     * @return void
     */
    public function handle(Saved $event)
    {
        $customer=$event->customer;
        $area=CustomerArea::find($customer->area_id);
        if ($area){
            $customer->area_format=$area->name;
        }
       $hangye=CustomerHangye::find($customer->hangye_id);
        if ($hangye){
            $customer->hangye_format=$hangye->name;
        }
        $peidian= CustomerPeidian::find($customer->peidian_id);
        if ($peidian){
            $customer->peidian_format=$peidian->name;
        }
        $xuqiu=CustomerXuqiu::find($customer->xuqiu_id);
        if ($xuqiu){
            $customer->xuqiu_format=$xuqiu->name;
        }
        $soure=CustomerSource::find($customer->source_id);
        if ($soure){
            $customer->source_format=$soure->name;
        }
        $huanping=CustomerHuanping::find($customer->huanping_id);
        if ($huanping){
            $customer->huanping_format=$huanping->name;
        }
        $wuye=CustomerWuye::find($customer->wuye_id);
        if ($wuye){
            $customer->wuye_format=$wuye->name;
        }
        $xiaofang=CustomerXiaofang::find($customer->xiaofang_id);
        if ($xiaofang){
            $customer->xiaofang_format=$xiaofang->name;
        }
        if (!$customer->code){
            $customer->code=Customer::getCode($customer);
        }
        if ($customer->record_time==null){
            $customer->record_time=Carbon::now();
        }
        $customer->save();
    }
}
